﻿using System;
//using System.Linq;
using System.Collections.Generic;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Client;
using System.Collections;
using Microsoft.WindowsMobile.PocketOutlook;

namespace Synchro
{
    public class Google
    {
        private static Google _google = null;
        private Credentials _credentials =new Credentials();
        private CalendarService _googleService = null;
        private List<CalendarEntry> _googleCalendars = new List<CalendarEntry>();
        private bool _connected = false;
        private string _calendarUri = null;

        private Google() { }

        public static Google getGoogle()
        {
            if (_google == null)
            {
                _google = new Google();
            }
            return _google;
        }

        public Credentials credentials
        {
            get { return _credentials; }
            set { _credentials = value; }
        }

        public string CalendarUri
        {
            get { return _calendarUri; }
            set { _calendarUri = value; }
        }

        public bool connect()
        {
            _googleService = new CalendarService("GoogleService");
            try
            {
                _googleService.setUserCredentials(_credentials.User, _credentials.Password);
                _connected = true;
                return true;
            }
            catch (AuthenticationException e)
            {
                return false;
            }
        }

        public bool isConnected()
        {
            return _connected;
        }

        public int insertEntry(EventEntry entry)
        {
            try
            {
                _googleService.Insert(new Uri(_calendarUri), entry);
                return 0;
            }catch(Exception e){
                return -1;
            }
        }

        public ArrayList updateCalendars()
        {
            _googleCalendars.Clear();
            ArrayList calendars = new ArrayList();
            bool success = connect();
            if (!success)
            {
                return null;
            }
            CalendarQuery query = new CalendarQuery("http://www.google.com/calendar/feeds/default/allcalendars/full");
            CalendarFeed resultFeed = (CalendarFeed)_googleService.Query(query);
            foreach (CalendarEntry entry in resultFeed.Entries)
            {
                calendars.Add(entry.Title.Text);
                _googleCalendars.Add(entry);
            }
            return calendars;
        }

        public void modifyUri(int index)
        {
            _calendarUri = _googleCalendars[index].EditUri.ToString().Replace("default/allcalendars/full/", "") + "/private/full";
        }

        public EventFeed getAllEntries(int index)
        {
            bool success = connect();
            EventQuery query = new EventQuery(_calendarUri);

            // Tell the service to query:
            EventFeed calFeed = _googleService.Query(query);
            return calFeed;
        }

        public bool appointmentExistsInGoogle(Appointment appt_searched, EventFeed appt_col)
        {
            DateTime start;
            DateTime end;
            start = appt_searched.Start;
            end = appt_searched.End;
            /*if (appt_searched.AllDayEvent) {
                 start = appt_searched.Start;
                 end = appt_searched.End;
            }
            else
            {
                start = appt_searched.Start.AddHours(-9);
                end = appt_searched.End.AddHours(-9);
            }*/
            string title = appt_searched.Subject;
            for (int i = 0; i < appt_col.Entries.Count; i++)
            {
                EventEntry appt = (EventEntry)appt_col.Entries[i];
                if (appt_searched.AllDayEvent)
                {
                    if ((start.Date.CompareTo(DateTime.Now) <= 0) || 
                        ((appt.Title.Text == title) && (appt.Times[0].AllDay.CompareTo(appt_searched.AllDayEvent) == 0)
                        && (appt.Times[0].StartTime.CompareTo(start.Date) == 0) ))
                    {
                        return true;
                    }
                }
                else //Citas con hora
                {

                    if ((start.CompareTo(DateTime.Now) <= 0) || 
                        ((appt.Title.Text == title) && (appt.Times[0].StartTime.CompareTo(start) == 0)
                        && (appt.Times[0].EndTime.CompareTo(end) == 0)))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
